//
//  XZShareController.m
//  StockSystem
//
//  Created by 贵州贵农网络科技有限公司 on 2018/8/14.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZShareController.h"
#import "GMBaseNavigationController.h"
#import "GMShareVC.h"
@interface XZShareController ()
@property (weak, nonatomic) IBOutlet UIView *bagView;

@end

@implementation XZShareController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (kIsEmptyString(self.type)) {
        self.bagView.hidden = NO;
    }else{
        self.bagView.hidden = YES;
    }
}
//空白处
- (IBAction)spaceViewTapClick:(id)sender {
    UIViewController * parent = [self.view containingViewController];
    if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
        [parent dismissSemiModalView];
    }
}
/**本站好友*/
- (IBAction)myFriend:(id)sender {
    if (kIsEmptyString(self.resId)) {
          [MBProgressHUD showErrorOrSucToWindow:@"不能分享给本站好友"];
    }else{
        GMShareVC *share = [[GMShareVC alloc] init];
        share.resId = self.resId;
        GMBaseNavigationController *baseNav = [[GMBaseNavigationController alloc] initWithRootViewController:share];
        [[UIViewController zj_currentNavigatonController] presentViewController:baseNav animated:YES completion:nil];
    }
}
//微信好友
- (IBAction)wechatFriendViewTapClick:(UITapGestureRecognizer *)sender {
    if (![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_WechatSession]) {
        [MBProgressHUD showErrorOrSuc:@"抱歉,没有安装微信客户端" toView:self.view];
        return;
    }
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    UMShareWebpageObject *shareObject = nil;
    shareObject = [UMShareWebpageObject shareObjectWithTitle:self.shareTitle descr:self.shareContent thumImage:kIsEmptyString(self.shareImgUrl)?[UIImage imageNamed:@"shareIcon"]:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.shareImgUrl]]]];
    //设置网页地址
    shareObject.webpageUrl = self.shareUrl;
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:self completion:^(id result, NSError *error) {
        //微信取消分享也不会走错误方法，都是成功
        UIViewController * parent = [self.view containingViewController];
        if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
            [parent dismissSemiModalView];
        }
    }];
}
//朋友圈
- (IBAction)momentViewTapClick:(UITapGestureRecognizer *)sender {
    if (![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_WechatTimeLine]) {
        [MBProgressHUD showErrorOrSuc:@"抱歉,没有安装微信客户端" toView:self.view];
        return;
    }
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    UMShareWebpageObject *shareObject = nil;
    shareObject = [UMShareWebpageObject shareObjectWithTitle:self.shareTitle descr:self.shareContent thumImage:kIsEmptyString(self.shareImgUrl)?[UIImage imageNamed:@"shareIcon"]:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.shareImgUrl]]]];
    //设置网页地址
    shareObject.webpageUrl = self.shareUrl;
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatTimeLine messageObject:messageObject currentViewController:self completion:^(id result, NSError *error) {
        UIViewController * parent = [self.view containingViewController];
        if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
            [parent dismissSemiModalView];
        }
    }];
}
//QQ好友
- (IBAction)qqFriendViewtTapClick:(UITapGestureRecognizer *)sender {
    if (![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ]) {
        [MBProgressHUD showErrorOrSuc:@"抱歉,没有安装QQ客户端" toView:self.view];
        return;
    }
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    UMShareWebpageObject *shareObject = nil;
    shareObject = [UMShareWebpageObject shareObjectWithTitle:self.shareTitle descr:self.shareContent thumImage:kIsEmptyString(self.shareImgUrl)?[UIImage imageNamed:@"shareIcon"]:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.shareImgUrl]]]];
    //设置网页地址
    shareObject.webpageUrl = self.shareUrl;
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_QQ messageObject:messageObject currentViewController:self completion:^(id result, NSError *error) {
        UIViewController * parent = [self.view containingViewController];
        if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
            [parent dismissSemiModalView];
        }
    }];
}
//新浪微博
- (IBAction)sinaViewTapClick:(UITapGestureRecognizer *)sender {
    if (![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_Sina]) {
        [MBProgressHUD showErrorOrSuc:@"抱歉,没有安装新浪客户端" toView:self.view];
        return;
    }
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    UMShareWebpageObject *shareObject = nil;
    shareObject = [UMShareWebpageObject shareObjectWithTitle:self.shareTitle descr:self.shareContent thumImage:kIsEmptyString(self.shareImgUrl)?[UIImage imageNamed:@"shareIcon"]:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.shareImgUrl]]]];
    //设置网页地址
    shareObject.webpageUrl = self.shareUrl;
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Sina messageObject:messageObject currentViewController:self completion:^(id result, NSError *error) {
//        if (error) {
//            [self handelShareResult:@"分享失败"];
//        }else{
//            [self handelShareResult:@"分享成功"];
//        }
        UIViewController * parent = [self.view containingViewController];
        if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
            [parent dismissSemiModalView];
        }
    }];
}
/**QQ空间*/
- (IBAction)qqzone:(id)sender {
    if (![[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_Qzone]) {
        [MBProgressHUD showErrorOrSuc:@"抱歉,没有安装QQ客户端" toView:self.view];
        return;
    }
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    UMShareWebpageObject *shareObject = nil;
    shareObject = [UMShareWebpageObject shareObjectWithTitle:self.shareTitle descr:self.shareContent thumImage:kIsEmptyString(self.shareImgUrl)?[UIImage imageNamed:@"shareIcon"]:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.shareImgUrl]]]];
    //设置网页地址
    shareObject.webpageUrl = self.shareUrl;
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Qzone messageObject:messageObject currentViewController:self completion:^(id result, NSError *error) {
        UIViewController * parent = [self.view containingViewController];
        if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
            [parent dismissSemiModalView];
        }
    }];
}


//取消
- (IBAction)cancleButtonClick:(UIButton *)sender {
    UIViewController * parent = [self.view containingViewController];
    if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
        [parent dismissSemiModalView];
    }
}
@end
